<?php
namespace app\manage\controller;

use app\manage\controller\BaseController;
use app\model\Log;
use app\Request;
use think\facade\View;
use think\facade\Db;
use think\exception\ValidateException;
use app\model\Admin;

class Login extends BaseController
{
    public function index(Request $request)
    {
        if(session('?uid')){
            return redirect(url('index/index'));
        }

    	if($request->isPost()){
    		$ip=$this->getIp();
    		$data=$request->post();
            try {
                $this->validate( $data,  [
                    'coder|验证码'=>'require|captcha',
                    'username|用户名'  => 'require|token',
                    'password|账号' => 'require'
                ]);
            } catch (ValidateException $e) {
               // dump($e->getError());
                $this->error('提交数据不正确');
            }
			//进行解密
            $password=base64_encode($data['password'].'lizhili');
			$data['username']=base64_decode($data['username']);
			$data['password']=base64_decode($data['password']);
			$log= Log::create([
                'username'=>$data['username'],
                'ip'=>$ip,
                'password'=>$password
            ]);
			$login=new Admin();
			$info=$login->login($data,$log->id);
			if($info){
				return $this->success('信息正确，正在跳转',url('index/index'));
			}else{
				return $this->error('用户名或密码错误');
			}
    	}
        View::assign('system',Db::name('system')->where('enname','webname')->value('value'));
        return View::fetch();
    }
	public function out()
    {
		session(null);
		return $this->success('退出成功','login/index');
    }
	
	 private function getIp(){
        if(!empty($_SERVER["HTTP_CLIENT_IP"])){
            $cip = $_SERVER["HTTP_CLIENT_IP"];
        }else if(!empty($_SERVER["HTTP_X_FORWARDED_FOR"])){
            $cip = $_SERVER["HTTP_X_FORWARDED_FOR"];
        }else if(!empty($_SERVER["REMOTE_ADDR"])){
            $cip = $_SERVER["REMOTE_ADDR"];
        }else{
            $cip = '';
        }
        preg_match("/[\d\.]{7,15}/", $cip, $cips);
        $cip = isset($cips[0]) ? $cips[0] : 'unknown';
        unset($cips);
        return $cip;
    }
	
	 public function log(){
	 	
        $data=input('get.');
         View::assign('key', $data);
        if(isset($data['start']) or isset($data['end'])){
            $map=[];
            if($data['start'] != ''){
                $map[]=	['create_time','>',strtotime($data['start'])];
            }
            if($data['end'] != ''){
                $map[]=	['create_time','<',strtotime($data['end'])+60*60*24];
            }
        }else{
            $map=true;
        }
        $data=Db::name('log')->where($map)->order('id','desc')->paginate(15,false,['query'=>request()->param()]);
         View::assign('data',$data);
		$count1=Db::name('log')->count();
         View::assign('count1', $count1);
	 	return View::fetch();
	 }
	public function ajax()
    {
    	$data=input('post.');
		if($data['type']=='log_del'){
			if(Db::name('log')->delete($data['id'])){
				return 1;//修改成功返回1
			}else{
				return 0;
			}
		}
		if($data['type']=='log_all'){
			if(Db::name('log')->delete($data['id'])){
				return 1;//修改成功返回1
			}else{
				return 0;
			}
		}
		return 0;
    }
}
